package hsp.admin.app.literature.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hsp.admin.api.ApiResult;
import hsp.admin.app.literature.po.CelebrityPO;
import hsp.admin.app.literature.service.ICelebrityService;
import hsp.admin.app.literature.vo.CelebrityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "CelebrityController", description = "名人管理")
@RestController
@RequestMapping("literature/celebrity")
public class CelebrityController {

    private final ICelebrityService service;

    @Autowired
    public CelebrityController(
        ICelebrityService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取名人")
    @GetMapping("{id}")
    public ApiResult<CelebrityPO> getCelebrity(@PathVariable("id") long id){
        CelebrityPO po = service.getById(id);
        return ApiResult.success(po);
    }

    @Operation(summary = "获取名人分页")
    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<CelebrityVO>> getCelebrityPage(@PathVariable int page, @PathVariable int size, CelebrityPO po){
        Page<CelebrityVO> records = service.pageVO(new Page<>(page, size), po);
        return ApiResult.success(records);
    }

    @Operation(summary = "编辑名人")
    @PostMapping()
    public ApiResult<?> postOrPutCelebrity(@RequestBody CelebrityPO po){
        service.saveOrUpdate(po);
        return ApiResult.success();
    }

    @Operation(summary = "删除名人")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteCelebrity(@PathVariable("id") long id){
        service.removeById(id);
        return ApiResult.success();
    }

    @Operation(summary = "根据名称获取名人")
    @GetMapping("list")
    public ApiResult<List<CelebrityPO>> getCelebrityListByName(CelebrityPO po){
        LambdaQueryWrapper<CelebrityPO> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(po.getName())) {
            wrapper
            .or().like(CelebrityPO::getName, po.getName())
            .or().like(CelebrityPO::getWord, po.getName())
            .or().like(CelebrityPO::getAliasName, po.getName())
            .or().like(CelebrityPO::getRealName, po.getName());
        }
        List<CelebrityPO> list = service.list(wrapper);
        return ApiResult.success(list);
    }

}
